package br.ufmg.vod.wbr.graphs;

import javolution.util.FastSet;

public class DirectedToUndirected implements GraphLoader<FastGraph> {

	private int[][] neighbours;

	public DirectedToUndirected(FastDirectedGraph graph) {
		this.neighbours = new int[graph.numVertex][];
		
		for (int i = 0; i < graph.numVertex; i++) {
			FastSet<Integer> set = new FastSet<Integer>();
			for (int j = 0; j < graph.inEdges[i].length; j++) {
				set.add(graph.inEdges[i][j]);
			}
			
			for (int j = 0; j < graph.outEdges[i].length; j++) {
				set.add(graph.outEdges[i][j]);
			}
			
			this.neighbours[i] = new int[set.size()];
			
			int j = 0;
			for (int v : set) {
				this.neighbours[i][j++] = v;
			}
		}
	}
	
	@Override
	public FastGraph loadGraph() throws Exception {
		return new FastGraph(neighbours);
	}

}
